﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibHuman;

namespace Human
{
	/// <summary>
	/// Summary description for frmRight.
	/// </summary>
	public class frmRight : System.Windows.Forms.Form
	{
		private AccessData m;
        Language lan = new Language();
		private System.Windows.Forms.ImageList imgList;
		private System.Windows.Forms.TextBox txtUserId;
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.CheckedListBox listBox1;
		private System.Windows.Forms.Button butThem;
		private System.Windows.Forms.Button butSua;
		private System.Windows.Forms.Button butHuy;
		private System.Windows.Forms.Button butKethuc;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button butAll;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.TextBox txtRight;
		private DataSet ds=new DataSet();
		private System.Windows.Forms.Button butCancel;
        private TextBox tim;
		private DataSet dsxml=new DataSet();
        bool m_admin = false;
		public frmRight(AccessData acc,bool admin)
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
			m=acc;
            m_admin = admin;
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmRight));
            this.imgList = new System.Windows.Forms.ImageList(this.components);
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.txtUserId = new System.Windows.Forms.TextBox();
            this.listBox1 = new System.Windows.Forms.CheckedListBox();
            this.label1 = new System.Windows.Forms.Label();
            this.txtRight = new System.Windows.Forms.TextBox();
            this.tim = new System.Windows.Forms.TextBox();
            this.butCancel = new System.Windows.Forms.Button();
            this.butAll = new System.Windows.Forms.Button();
            this.butKethuc = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butThem = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // imgList
            // 
            this.imgList.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imgList.ImageStream")));
            this.imgList.TransparentColor = System.Drawing.Color.Transparent;
            this.imgList.Images.SetKeyName(0, "");
            this.imgList.Images.SetKeyName(1, "");
            this.imgList.Images.SetKeyName(2, "");
            // 
            // dataGrid1
            // 
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.CaptionForeColor = System.Drawing.SystemColors.Highlight;
            this.dataGrid1.ColumnHeadersVisible = false;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dataGrid1.Location = new System.Drawing.Point(10, 5);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.SelectionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.Size = new System.Drawing.Size(190, 405);
            this.dataGrid1.TabIndex = 0;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            // 
            // txtUserId
            // 
            this.txtUserId.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txtUserId.Enabled = false;
            this.txtUserId.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtUserId.Location = new System.Drawing.Point(204, 416);
            this.txtUserId.Name = "txtUserId";
            this.txtUserId.Size = new System.Drawing.Size(201, 23);
            this.txtUserId.TabIndex = 8;
            // 
            // listBox1
            // 
            this.listBox1.CheckOnClick = true;
            this.listBox1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.listBox1.Location = new System.Drawing.Point(205, 5);
            this.listBox1.Name = "listBox1";
            this.listBox1.Size = new System.Drawing.Size(582, 404);
            this.listBox1.TabIndex = 2;
            this.listBox1.SelectedValueChanged += new System.EventHandler(this.listBox1_SelectedValueChanged);
            // 
            // label1
            // 
            this.label1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.Red;
            this.label1.Location = new System.Drawing.Point(493, 416);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(285, 23);
            this.label1.TabIndex = 12;
            this.label1.Text = "label1";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.label1.Visible = false;
            // 
            // txtRight
            // 
            this.txtRight.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txtRight.Enabled = false;
            this.txtRight.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.txtRight.Location = new System.Drawing.Point(552, 416);
            this.txtRight.Name = "txtRight";
            this.txtRight.Size = new System.Drawing.Size(204, 23);
            this.txtRight.TabIndex = 13;
            this.txtRight.Visible = false;
            // 
            // tim
            // 
            this.tim.BackColor = System.Drawing.SystemColors.HighlightText;
            this.tim.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tim.ForeColor = System.Drawing.Color.Red;
            this.tim.Location = new System.Drawing.Point(9, 416);
            this.tim.Name = "tim";
            this.tim.Size = new System.Drawing.Size(191, 21);
            this.tim.TabIndex = 15;
            this.tim.Text = "Tìm kiếm";
            this.tim.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.tim.Enter += new System.EventHandler(this.tim_Enter);
            this.tim.TextChanged += new System.EventHandler(this.tim_TextChanged);
            // 
            // butCancel
            // 
            this.butCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butCancel.Image = global::Human.Properties.Resources.Cancel;
            this.butCancel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butCancel.Location = new System.Drawing.Point(426, 445);
            this.butCancel.Name = "butCancel";
            this.butCancel.Size = new System.Drawing.Size(85, 28);
            this.butCancel.TabIndex = 14;
            this.butCancel.Text = "Hủy tất cả";
            this.butCancel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butCancel.Click += new System.EventHandler(this.butCancel_Click);
            // 
            // butAll
            // 
            this.butAll.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butAll.Image = global::Human.Properties.Resources.ok;
            this.butAll.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butAll.Location = new System.Drawing.Point(335, 445);
            this.butAll.Name = "butAll";
            this.butAll.Size = new System.Drawing.Size(85, 28);
            this.butAll.TabIndex = 5;
            this.butAll.Text = "Chọn tất cả";
            this.butAll.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butAll.Click += new System.EventHandler(this.butAll_Click);
            // 
            // butKethuc
            // 
            this.butKethuc.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.butKethuc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKethuc.ForeColor = System.Drawing.SystemColors.WindowText;
            this.butKethuc.Image = global::Human.Properties.Resources.close_r;
            this.butKethuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKethuc.Location = new System.Drawing.Point(583, 445);
            this.butKethuc.Name = "butKethuc";
            this.butKethuc.Size = new System.Drawing.Size(70, 28);
            this.butKethuc.TabIndex = 7;
            this.butKethuc.Text = "Kết thúc";
            this.butKethuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKethuc.Click += new System.EventHandler(this.butKethuc_Click);
            // 
            // butHuy
            // 
            this.butHuy.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butHuy.ForeColor = System.Drawing.SystemColors.WindowText;
            this.butHuy.Image = global::Human.Properties.Resources.DeleteFilter;
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(269, 445);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(60, 28);
            this.butHuy.TabIndex = 4;
            this.butHuy.Text = "Hủy";
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // butSua
            // 
            this.butSua.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butSua.ForeColor = System.Drawing.SystemColors.WindowText;
            this.butSua.Image = global::Human.Properties.Resources.quick_edit;
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(517, 445);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(60, 28);
            this.butSua.TabIndex = 6;
            this.butSua.Text = "Sửa";
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butThem
            // 
            this.butThem.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butThem.ForeColor = System.Drawing.SystemColors.WindowText;
            this.butThem.Image = global::Human.Properties.Resources.add;
            this.butThem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThem.Location = new System.Drawing.Point(203, 445);
            this.butThem.Name = "butThem";
            this.butThem.Size = new System.Drawing.Size(60, 28);
            this.butThem.TabIndex = 3;
            this.butThem.Text = "Thêm";
            this.butThem.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butThem.Click += new System.EventHandler(this.butThem_Click);
            // 
            // frmRight
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(790, 481);
            this.Controls.Add(this.tim);
            this.Controls.Add(this.butCancel);
            this.Controls.Add(this.listBox1);
            this.Controls.Add(this.txtRight);
            this.Controls.Add(this.butAll);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.dataGrid1);
            this.Controls.Add(this.butKethuc);
            this.Controls.Add(this.butHuy);
            this.Controls.Add(this.butSua);
            this.Controls.Add(this.butThem);
            this.Controls.Add(this.txtUserId);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmRight";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Phân quyền sử dụng";
            this.Load += new System.EventHandler(this.frmRight_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void AddGridTableStyle()
		{
			dataGrid1.TableStyles.Clear();
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = ds.Tables[0].TableName;
			ts.RowHeadersVisible=false;
			ts.ColumnHeadersVisible=false;
			ts.GridLineColor= Color.White;

			DataGridIconColumn  iconColumn = new DataGridIconColumn(this.imgList,new delegateGetIconIndexForRow(MyGetImageIndexForRow)); //0
			iconColumn.HeaderText = "";
			iconColumn.MappingName = "Icon";
			iconColumn.Width = this.imgList.Images[0].Size.Width;
			ts.GridColumnStyles.Add(iconColumn);
			dataGrid1.TableStyles.Add(ts);
			
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn(); //1
            TextCol1.MappingName = "username";
			TextCol1.HeaderText = "User ID";
			TextCol1.Width = 150;
			ts.GridColumnStyles.Add(TextCol1);
			dataGrid1.TableStyles.Add(ts);		

			TextCol1=new DataGridTextBoxColumn(); //2
			TextCol1.MappingName = "pass";
			TextCol1.HeaderText = "";
			TextCol1.Width = 0;
			ts.GridColumnStyles.Add(TextCol1);
			dataGrid1.TableStyles.Add(ts);	

			TextCol1=new DataGridTextBoxColumn(); //3
			TextCol1.MappingName = "_right";
			TextCol1.HeaderText = "";
			TextCol1.Width = 0;
			ts.GridColumnStyles.Add(TextCol1);
			dataGrid1.TableStyles.Add(ts);	

		}

		public delegate int delegateGetIconIndexForRow(int row);
		public class DataGridIconColumn : DataGridTextBoxColumn
		{
			private ImageList _icons;
			delegateGetIconIndexForRow _getIconIndex;
		
			public DataGridIconColumn(ImageList Icons, delegateGetIconIndexForRow getIconIndex)
			{
				_icons = Icons;
				_getIconIndex = getIconIndex;
			}

			protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible) 
			{ 
				//do not allow the unbound cell to become active
				if(this.MappingName == "Icon")
					return; 
 
				base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible); 
			} 

			protected override void Paint(System.Drawing.Graphics g, System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush foreBrush, bool alignToRight)
			{
				try
				{
					//erase background
					g.FillRectangle(backBrush, bounds);
					g.DrawImage(this._icons.Images[_getIconIndex(rowNum)], bounds);
				}
			
				catch{ /* empty catch */ }
			}
		}

		public int MyGetImageIndexForRow(int row)
		{	
			return row % 3;
		}

		private void frmRight_Load(object sender, System.EventArgs e)
		{
            dsxml.ReadXml("..\\xml\\m_menubar.xml");
            listBox1.DataSource = dsxml.Tables[0];
            listBox1.DisplayMember = "ten";
            listBox1.ValueMember = "id";
            load_grid();
			AddGridTableStyle();			
			ref_text();
        }
        #region code cu
        private void load_grid()
        {
            ds = new DataSet();
            ds = m.get_data("select * from " + m.user + ".pwhuman" + " order by username");
            ds.Tables[0].Columns.Add("Icon");
            dataGrid1.DataSource = ds.Tables[0];
        }

		private void butKethuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}		
	
		private void gan_right(string s)
		{
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                if (s.IndexOf(dsxml.Tables[0].Rows[i]["id"].ToString().PadLeft(5, '0') + "+") != -1)
                    listBox1.SetItemCheckState(i, CheckState.Checked);
                else
                    listBox1.SetItemCheckState(i, CheckState.Unchecked);
            }
			label1.Text=" Quyền truy cập "+listBox1.CheckedItems.Count.ToString()+" / "+listBox1.Items.Count.ToString();
		}

		private void luu_right()
		{
			try
			{
                m.upd_pwhuman(txtUserId.Text ,txtRight.Text);
                m.updrec(ds.Tables[0],dataGrid1[dataGrid1.CurrentCell.RowNumber,1].ToString(),"",txtRight.Text);              
                
			}
			catch{}
		}

		private void butThem_Click(object sender, System.EventArgs e)
		{
            frmUser f=new frmUser(m,ds,"","","");
            f.ShowDialog();
            load_grid();
            
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
            frmUser f=new frmUser(m,ds,dataGrid1[dataGrid1.CurrentCell.RowNumber,1].ToString(),dataGrid1[dataGrid1.CurrentCell.RowNumber,2].ToString(),dataGrid1[dataGrid1.CurrentCell.RowNumber,3].ToString());
            f.ShowDialog();	
            ds=f.dsright;
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
            
			if (MessageBox.Show(lan.Change_language_MessageText("Đồng ý hủy?"),AccessData.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
                m.Execute_data("delete from " + m.user + ".pwhuman where username='" + txtUserId.Text + "'");
                m.delrec(ds.Tables[0], "username='" + txtUserId.Text + "'");
				ds.AcceptChanges();
			}
		}

		private void gan_text()
		{
            string s = "";
            for (int i = 0; i < listBox1.Items.Count; i++)
                if (listBox1.GetItemChecked(i)) s += dsxml.Tables[0].Rows[i]["id"].ToString().PadLeft(5, '0') + "+";
			txtRight.Text=s;
			luu_right();
            load_grid();
			label1.Text=lan.Change_language_MessageText(" Quyền truy cập ")+listBox1.CheckedItems.Count.ToString()+" / "+listBox1.Items.Count.ToString();
		}

		private void listBox1_SelectedValueChanged(object sender, System.EventArgs e)
		{
            if (this.ActiveControl == listBox1) gan_text();
		}

		private void butAll_Click(object sender, System.EventArgs e)
		{
			string s="";
			for(int i=0;i<listBox1.Items.Count;i++)
			{
                listBox1.SetItemCheckState(i, CheckState.Checked);
                listBox1.GetItemCheckState(i);
                s+=dsxml.Tables[0].Rows[i]["id"].ToString().PadLeft(5,'0')+ "+";
				//s+=i.ToString().PadLeft(5,'0')+"+";
			}
			txtRight.Text=s;
			luu_right();
            load_grid();
			label1.Text=lan.Change_language_MessageText("Quyền truy cập ")+listBox1.CheckedItems.Count.ToString()+" / "+listBox1.Items.Count.ToString();
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
            ref_text();
		}

		private void ref_text()
		{
			try
			{
				gan_right(dataGrid1[dataGrid1.CurrentCell.RowNumber,3].ToString());
				txtUserId.Text=dataGrid1[dataGrid1.CurrentCell.RowNumber,1].ToString();
			}
			catch{}
		}

		private void butCancel_Click(object sender, System.EventArgs e)
		{
			for(int i=0;i<listBox1.Items.Count;i++) listBox1.SetItemCheckState(i,CheckState.Unchecked);
			txtRight.Text="";
			luu_right();
			label1.Text=lan.Change_language_MessageText("Quyền truy cập ")+listBox1.CheckedItems.Count.ToString()+" / "+listBox1.Items.Count.ToString();
		}

        private void tim_Enter(object sender, System.EventArgs e)
        {
            tim.Text = "";
        }

        private void tim_TextChanged(object sender, System.EventArgs e)
        {
            if (this.ActiveControl == tim)
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[dataGrid1.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = " username like '%" + tim.Text.Trim() + "%'";
            }
        }
        #endregion 
        
    }
}
